<?php

namespace app\product\logic;

use think\facade\Db;
use think\facade\Cache;
use util\Excel;
use think\exception\ValidateException;
use think\facade\Log;
use util\Token;
use util\Upload;
use think\cache\driver\Redis;

class ProductFeedbackLogic
{
    public function __construct()
    {

    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 列表
     * @author zhangheng
     */
    public function ProductFeedbackList($params = [])
    {
        $spu = $params['spu'] ?? [];
        $limit = $params['limit'] ?? 10;
        $where = [];

        if(!empty($spu)){
            $where['a.spu'] = $spu;
        }
        
        $list = Db::name('fedition_reportmonthtwo')
            ->field('a.id,a.advertiser_id,a.platform_id,a.advertiser_name,
                b.shop_id,
                b.createtime,
                b.updatetime,a.status,
                c.nickname as admin_name,
                d.name as shop_name,
                e.title as platform_title
            ')
            ->alias('a')
            ->join('qu_advaccount_admin b','a.advertiser_id = b.advertiser_id', 'left')
            ->join('qu_system_admin c','c.id = b.admin_id', 'left')
            ->join('qu_shops d','d.id = b.shop_id', 'left')
            ->join('qu_advplatform e','e.id = a.platform_id', 'left')
            ->where($where)
            ->order("b.updatetime desc")
            ->paginate($limit);

        $lists = $list->items();
        foreach ($lists as $k=>$v) {
            if(!empty($v['createtime'])){
                $lists[$k]['createtime'] = date('Y-m-d',$v['createtime']);
                if(!empty($v['updatetime'])){
                    $lists[$k]['updatetime'] = date('Y-m-d', strtotime($v['updatetime']));
                }else{
                    $lists[$k]['updatetime'] = "至今";
                }
            }else{
                $lists[$k]['createtime'] = "";
                $lists[$k]['updatetime'] = "";
            }
        }
                
        $data = [
            'list' => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 导出
     * @author zhangheng
     */
    public function export($params = [])
    {
        $lists = [];
        $header = [];
        // $header[] = ['广告账户', 'advertiser_id'];
        $fileName = "export_Advaccount" . time();
        return Excel::exportData($lists, $header, $fileName, 'xlsx');
    }

    
}
